<template>
  <BaseLayout title="流程分组管理" @query="init" @reset="reset">
    <template #query>
      <el-form :inline="true" :model="queryForm">
        <el-form-item label="分组名称">
          <el-input clearable v-model="queryForm.groupName" placeholder="请输入分组名称"/>
        </el-form-item>

        <el-form-item label="分组编号">
          <el-input clearable v-model="queryForm.groupCode" placeholder="请输入分组编号"/>
        </el-form-item>

      </el-form>
    </template>
    <template #button>
      <el-button type="primary" @click="addFlowGroup">新增</el-button>
    </template>
    <template #table>
      <el-table v-loading="loading" :data="data" height="100%" class="w-full">
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="groupName" label="分组名称" align="center" header-align="center"></el-table-column>
        <el-table-column prop="groupCode" label="分组编号" align="center" header-align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center" header-align="center"></el-table-column>
        <el-table-column prop="createUserName" label="创建用户" align="center" header-align="center"></el-table-column>
        <el-table-column header-align="center" align="center" prop="enabled" label="是否启用" width="80">
          <template #default="scope">
            <BaseTag :value="scope.row.enabled" :dict="enableOptions"></BaseTag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" header-align="center">
          <template #default="scope">
            <el-button link type="primary" @click="editFlowGroup(scope.row.groupId)">修改</el-button>
            <el-button link type="danger" @click="delFlowGroup(scope.row.groupId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddOrUpdateFlowGroup @load="init" ref="addOrUpdateFlowGroupRef"></AddOrUpdateFlowGroup>
    </template>
    <template #page>
      <BasePage @load="init" v-model:current="queryForm.current" v-model:size="queryForm.size"
                :total="queryForm.total"></BasePage>
    </template>
  </BaseLayout>
</template>

<script setup lang="ts">
import AddOrUpdateFlowGroup from './AddOrUpdateFlowGroup.vue'
import {nextTick, onMounted, reactive, ref} from "vue";
import {deleteFlowGroupByGroupId, listFlowGroupPage, selectFlowGroupByGroupId} from "/@/api/flow/FlowGroupApi";
import {messageBox} from "/@/utils/BjxUtils";
import {DictDataModel} from "/@/api/system/model/DictModel";
import {FlowGroupQueryModel, FlowGroupModel} from "/@/api/flow/model/FlowGroupModel";
import {ElMessage} from "element-plus";
import {useDictStore} from "/@/store/modules/dict";

const enableOptions = ref<DictDataModel[]>([])
const data = ref<FlowGroupModel[]>([])
const addOrUpdateFlowGroupRef = ref()
const loading = ref(false)
const queryForm = reactive<FlowGroupQueryModel>({
  groupName: '',
  groupCode: '',
  size: 10,
  current: 1,
  total: 0
})
const init = async () => {
  loading.value = true
  let res = await listFlowGroupPage({...queryForm}, () => {
    loading.value = false
  })
  data.value = res.records
  queryForm.size = res.size
  queryForm.current = res.current
  queryForm.total = res.total
}
/**
 * 新增流程分组
 */
const addFlowGroup = () => {
  addOrUpdateFlowGroupRef.value.init("新增流程分组")
}
/**
 * 修改流程分组
 */
const editFlowGroup = async (groupId: string) => {
  loading.value = true
  let res = await selectFlowGroupByGroupId(groupId)
  addOrUpdateFlowGroupRef.value.init("修改流程分组", res)
  loading.value = false
}
/**
 * 删除流程分组
 */
const delFlowGroup = async (groupId: string) => {
  messageBox("您确定要删除这个流程分组吗?", async () => {
    loading.value = true
    await deleteFlowGroupByGroupId(groupId, () => {
      loading.value = false
    })
    init()
  })

}
/**
 * 重置
 */
const reset = () => {
  queryForm.groupName = '',
      queryForm.groupCode = '',
      queryForm.current = 1
  init()
}
onMounted(async () => {
  let dictStore = useDictStore();
  enableOptions.value = await dictStore.getDict('enable_dict')
  nextTick(() => {
    init()
  })

})
</script>

<style scoped>

</style>